﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace WebCycle
{
    class ScreensaverConfiguration
    {
        public List<string> Urls
        {
            get
            {
                return _urls;
            }
        }

        public int Delay
        {
            get
            {
                return _delay * 1000;
            }
        }

        private List<string> _urls = new List<string>();
        private int _delay;

        public ScreensaverConfiguration() 
        {
            LoadConfigFromRegistry();
        }

        private void LoadConfigFromRegistry()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\WebCycle");
            if (key == null)
                _delay = 5;
            else
            {
                foreach (string url in System.Text.RegularExpressions.Regex.Replace((string)key.GetValue("urls"), @"[\r\n]", "").Split(','))
                {
                    if (!string.IsNullOrEmpty(url.Trim()))
                        _urls.Add(url.Trim());
                }
                _delay = int.Parse(key.GetValue("delay").ToString());
            }

            if (_urls.Count == 0)
                _urls.Add("http://timlaqua.com/WebCycle");
        }

        internal void SaveConfigToRegistry(string Urls, int Delay)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\WebCycle");
            if (key != null)
                Registry.CurrentUser.DeleteSubKeyTree("SOFTWARE\\WebCycle");

            key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\WebCycle");

            key.SetValue("urls", Urls);
            key.SetValue("delay", Delay.ToString());
        }
    }
}
